/*
 * FamilyAdministrator.java
 *
 * Created on September 11, 2008, 10:18 AM
 */

package cl.b9.socialNetwork.gui;

import cl.b9.socialNetwork.GraphPanel;
import cl.b9.socialNetwork.SNDirector;
import cl.b9.socialNetwork.model.SNFamily;
import java.awt.Color;
import javax.swing.JColorChooser;
import org.apache.log4j.Logger;

/**
 *
 * @author  manuel
 */
public class FamilyAdministrator extends javax.swing.JDialog {

    private SNFamily selectedFamily = null;
    private Logger logger = Logger.getLogger(FamilyAdministrator.class);
    private boolean dirty = false;
    
    /** Creates new form FamilyAdministrator */
    public FamilyAdministrator(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.familyList.setSelectedIndex(this.familyList.getFirstVisibleIndex());
        GraphPanel gp = new GraphPanel(parent,200,100);
    /*    SNActor triangle = new SNActor("1",null);
        triangle.setShape(SNShape.TRIANGLE);
        SNActor rectangle = new SNActor("2",null);
        rectangle.setShape(SNShape.RECTANGLE);
        SNActor star = new SNActor("3",null);
        star.setShape(SNShape.STAR);
        SNActor circle = new SNActor("4",null);
        circle.setShape(SNShape.CIRCLE);
        gp.add(triangle, new Point(20,20));
        gp.add(rectangle,new Point(50,20));
        gp.add(star, new Point(80,20));
        gp.add(circle,new Point(110,20));
     * */
        shapePanel.add(gp.getViewer());
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        familyGroupType = new javax.swing.ButtonGroup();
        familySelector = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        familyList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        familyConfigurationPanel = new javax.swing.JPanel();
        colorPanel = new javax.swing.JPanel();
        colorPreview = new javax.swing.JButton();
        shapePanel = new javax.swing.JPanel();
        sizePanel = new javax.swing.JPanel();
        jSlider1 = new javax.swing.JSlider();
        labelPanel = new javax.swing.JPanel();
        labelFamily = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        btnUpdate = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cl.b9.socialNetwork.SocialNetworksApp.class).getContext().getResourceMap(FamilyAdministrator.class);
        familySelector.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("familySelector.border.title"))); // NOI18N
        familySelector.setName("familySelector"); // NOI18N

        familyGroupType.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.setName("jRadioButton1"); // NOI18N

        familyGroupType.add(jRadioButton2);
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setName("jRadioButton2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        familyList.setName("familyList"); // NOI18N
        familyList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                familyListValueChanged(evt);
            }
        });
        familyList.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                familyListPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(familyList);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout familySelectorLayout = new javax.swing.GroupLayout(familySelector);
        familySelector.setLayout(familySelectorLayout);
        familySelectorLayout.setHorizontalGroup(
            familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, familySelectorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(familySelectorLayout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton2))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, familySelectorLayout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        familySelectorLayout.setVerticalGroup(
            familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(familySelectorLayout.createSequentialGroup()
                .addGroup(familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addGroup(familySelectorLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2))
                        .addGap(9, 9, 9)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(familySelectorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        familyConfigurationPanel.setName("familyConfigurationPanel"); // NOI18N
        familyConfigurationPanel.setLayout(new java.awt.GridLayout(2, 2));

        colorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("colorPanel.border.title"))); // NOI18N
        colorPanel.setName("colorPanel"); // NOI18N

        colorPreview.setBackground(resourceMap.getColor("colorPreview.background")); // NOI18N
        colorPreview.setText(resourceMap.getString("colorPreview.text")); // NOI18N
        colorPreview.setName("colorPreview"); // NOI18N
        colorPreview.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colorPreviewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout colorPanelLayout = new javax.swing.GroupLayout(colorPanel);
        colorPanel.setLayout(colorPanelLayout);
        colorPanelLayout.setHorizontalGroup(
            colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, colorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(colorPreview, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addContainerGap())
        );
        colorPanelLayout.setVerticalGroup(
            colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(colorPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(colorPreview, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        colorPreview.setBackground(Color.red);

        familyConfigurationPanel.add(colorPanel);

        shapePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("shapePanel.border.title"))); // NOI18N
        shapePanel.setName("shapePanel"); // NOI18N
        shapePanel.setLayout(new java.awt.BorderLayout());
        familyConfigurationPanel.add(shapePanel);

        sizePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("sizePanel.border.title"))); // NOI18N
        sizePanel.setName("sizePanel"); // NOI18N

        jSlider1.setMaximum(50);
        jSlider1.setMinimum(1);
        jSlider1.setValue(20);
        jSlider1.setName("jSlider1"); // NOI18N

        javax.swing.GroupLayout sizePanelLayout = new javax.swing.GroupLayout(sizePanel);
        sizePanel.setLayout(sizePanelLayout);
        sizePanelLayout.setHorizontalGroup(
            sizePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, sizePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addContainerGap())
        );
        sizePanelLayout.setVerticalGroup(
            sizePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(sizePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        familyConfigurationPanel.add(sizePanel);

        labelPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("labelPanel.border.title"))); // NOI18N
        labelPanel.setName("labelPanel"); // NOI18N

        labelFamily.setText(resourceMap.getString("labelFamily.text")); // NOI18N
        labelFamily.setName("labelFamily"); // NOI18N

        jCheckBox1.setSelected(true);
        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        javax.swing.GroupLayout labelPanelLayout = new javax.swing.GroupLayout(labelPanel);
        labelPanel.setLayout(labelPanelLayout);
        labelPanelLayout.setHorizontalGroup(
            labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(labelPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelFamily, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                    .addComponent(jCheckBox1))
                .addContainerGap())
        );
        labelPanelLayout.setVerticalGroup(
            labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(labelPanelLayout.createSequentialGroup()
                .addComponent(labelFamily, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        familyConfigurationPanel.add(labelPanel);

        btnUpdate.setText(resourceMap.getString("btnUpdate.text")); // NOI18N
        btnUpdate.setName("btnUpdate"); // NOI18N
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(familySelector, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(familyConfigurationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
                .addComponent(btnClose)
                .addGap(127, 127, 127))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(familySelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(familyConfigurationPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnUpdate))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void colorPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colorPreviewActionPerformed
  JColorChooser jcc = new JColorChooser();
  Color newColor = JColorChooser.showDialog(
                     this,
                     "Choose Background Color",
                     colorPreview.getBackground());
  if (newColor != null){
      colorPreview.setBackground(newColor);
      dirty = true;
  }                  
}//GEN-LAST:event_colorPreviewActionPerformed

private void familyListPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_familyListPropertyChange
    
}//GEN-LAST:event_familyListPropertyChange

private void familyListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_familyListValueChanged
    if (this.selectedFamily == (SNFamily)familyList.getSelectedValue()){
        return;
    }
    dirty = false;
    this.selectedFamily = (SNFamily)familyList.getSelectedValue();
    this.refreshFamilyProps();
    logger.debug("Selected family is " + this.selectedFamily);
}//GEN-LAST:event_familyListValueChanged

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
    //TODO: Preguntar si deseo guardar los cambios antes de cerrar
    if (this.dirty){
        
    }
    this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
    selectedFamily.setColor(colorPreview.getBackground());
    selectedFamily.setLabel(this.labelFamily.getText());
    SNDirector.getInstance().repaint();
    dirty = false;
}//GEN-LAST:event_btnUpdateActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JPanel colorPanel;
    private javax.swing.JButton colorPreview;
    private javax.swing.JPanel familyConfigurationPanel;
    private javax.swing.ButtonGroup familyGroupType;
    private javax.swing.JList familyList;
    private javax.swing.JPanel familySelector;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField labelFamily;
    private javax.swing.JPanel labelPanel;
    private javax.swing.JPanel shapePanel;
    private javax.swing.JPanel sizePanel;
    // End of variables declaration//GEN-END:variables

    private void refreshFamilyProps() {
       colorPreview.setBackground(selectedFamily.getColor());
       labelFamily.setText(selectedFamily.getLabel());
    }

}
